package arrayList;

import java.util.Arrays;

public class MyArrayList implements IList{

    public int[] elem;
    public int usedSize;

    public MyArrayList(){
        this.elem = new int[10];
    }

    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }

//    @Override
//    private boolean isFull(){
//        return usedSize == elem.length;
//    }

    private void expandCapacity(){
        this.elem = Arrays.copyOf(this.elem,2*elem.length);
    }

    @Override
    public void add(int data) {
        if(isFull()){
            expandCapacity();
        }
        this.elem[usedSize] = data;
        this.usedSize++;
    }

    private void checkPosOfAdd(int pos) throws PosNotLegalException{
        if(pos < 0 || pos > usedSize) {
            throw new PosNotLegalException("pos位置不合法!");
        }
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPosOfAdd(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        if(isFull()){
            expandCapacity();
        }
        //移动元素
        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private void checkPosOfGetAndSet(int pos) throws PosNotLegalException{
        if(pos < 0 || pos >= usedSize) {
            throw new PosNotLegalException("get/set获取元素的时候" +
                    "pos位置不合法！");
        }
    }

    @Override
    public int get(int pos) {
        try {
            checkPosOfGetAndSet(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkPosOfGetAndSet(pos);
        }catch (PosNotLegalException e){
            e.printStackTrace();
        }
        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        //先检查该数字是否存在
        int pos = indexOf(toRemove);
        if(pos == -1){
            System.out.println("没有要删除的数字");
            return;
        }
        for (int i = pos; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

}
